home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / basic / imb9006.zip / FACTOR.BAS next >
BASIC Source File  |  1990-05-16  |  4KB  |  174 lines

  1. DEFINT A-Z
  2. DECLARE FUNCTION TDiff! (TA!, TB!)
  3. DECLARE FUNCTION Factorial# (I%)
  4.  
  5. ' FACTOR - Lookup table demonstration using the
  6. ' factorial series.
  7.  
  8. CONST BigFact = 170 'largest factorial for doubles
  9. CONST SecDay& = 24 * 60 * 60&
  10.  
  11. DIM Fact(0 TO BigFact) AS DOUBLE
  12. DIM t1 AS SINGLE, t2 AS SINGLE, t3 AS SINGLE
  13.  
  14. ' STEP 1 - compute each factorial nTrial times
  15.  
  16. INPUT "How many trials do you want (1 to 99)? ",_
  17.       NTrials
  18.  
  19. CLS
  20. PRINT "Step 1"
  21. PRINT USING _
  22.   "Now computing each factorial ## times"; NTrials
  23.  
  24. t1 = TIMER
  25. FOR I = 1 TO NTrials
  26.     FOR J = 0 TO BigFact
  27.         F# = Factorial#(J)            ' f=j!
  28.     NEXT
  29. NEXT
  30. t2 = TIMER
  31.  
  32. PRINT "Computing all factorials from 0..170"
  33. PRINT USING "## times took ###.## seconds.";_
  34.       NTrials; TDiff(t1, t2)
  35.  
  36. ' STEP 2 - compute the table, then look up
  37. '          each factorial nTrial times
  38.  
  39. PRINT : PRINT "Step 2"
  40. PRINT "Now compute table and look up each ";
  41. PRINT USING "factorial ## times."; NTrials
  42.  
  43. t1 = TIMER
  44.  
  45. FOR I = 0 TO BigFact
  46.     Fact(I) = Factorial#(I)
  47. NEXT
  48.  
  49. t2 = TIMER
  50.  
  51. FOR I = 1 TO NTrials
  52.     FOR J = 0 TO BigFact
  53.         F# = Fact(J)                 ' f=j!
  54.     NEXT
  55. NEXT
  56.  
  57. t3 = TIMER
  58.  
  59. PRINT USING "Computing table took ###.## secs.";_
  60.        TDiff(t1, t2)
  61. PRINT USING "Looking up each factorial ##";NTrials;
  62. PRINT USING " times took ###.## seconds.";_
  63.        TDiff(t2, t3)
  64. PRINT USING "Total: ###.## seconds"; TDiff(t1, t3)
  65.  
  66. 'STEP 3 - Compute each factorial as it is needed
  67.  
  68. PRINT : PRINT "Step 3"
  69. PRINT "Clearing the table, and computing each "
  70. PRINT USING_
  71.    "factorial as it is needed (for ##) lookups.";_
  72.      NTrials
  73.  
  74. t1 = TIMER
  75. FOR I = 0 TO BigFact
  76.     Fact(I) = -1        'unknown value
  77. NEXT
  78.  
  79. FOR I = 1 TO NTrials
  80.     FOR J = 0 TO BigFact
  81.         F# = Fact(J)     'lookup
  82.         IF F# < 0 THEN         'Check if valid
  83.             Fact(J) = Factorial#(J)
  84.         END IF
  85.     NEXT
  86. NEXT
  87. t2 = TIMER
  88.  
  89. PRINT "Clearing table and computing each"
  90. PRINT USING "factorial as it was needed for ##";_
  91.      NTrials
  92. PRINT USING "lookups took ###.## seconds.";_
  93.      TDiff(t1, t2)
  94.  
  95. ' STEP 4 - write the table to disk (we are
  96. '   not timing this step, because if you are
  97. '   loading it from disk, you presumably don't
  98. '   care how long it took to compute it.
  99.  
  100. OPEN "Facts.Tmp" FOR RANDOM AS 1
  101. FOR I = 1 TO BigFact
  102.     PUT #1, I, Fact(I)
  103. NEXT I
  104. CLOSE 1
  105.  
  106. ' Flush the disk buffer, so that the time
  107. ' is not affected by having the data in a
  108. ' disk buffer.
  109.  
  110. SHELL "CHkdsk > nul:"
  111.  
  112. ' STEP 5 - read the table from disk, and
  113. '          use each factorial 5 times
  114.  
  115. t1 = TIMER
  116. OPEN "FACTS.TMP" FOR RANDOM AS 1
  117. FOR I = 1 TO BigFact
  118.     GET #1, I, Fact(I)
  119. NEXT I
  120. CLOSE 1
  121. t2 = TIMER
  122.  
  123. FOR I = 1 TO NTrials
  124.     FOR J = 0 TO BigFact
  125.         F# = Fact(J)                 ' f=j!
  126.     NEXT
  127. NEXT
  128.  
  129. t3 = TIMER
  130. PRINT : PRINT "Step 5"
  131. PRINT USING_
  132.   "Reading table from disk took ###.## secs.";_
  133.    TDiff(t1, t2)
  134. PRINT USING_
  135.   "Looking up each factorial ## times";NTrials;
  136. PRINT USING_
  137.   " took ###.## seconds."; TDiff(t2, t3)
  138. PRINT USING "Total: ###.## seconds"; TDiff(t1, t3)
  139.  
  140. '************************************************
  141. ' Factorial - compute the factorial of a number *
  142. '
  143. ' INP:   i - the # to compute the factorial of
  144. ' OUT:     The factorial of the number, unless a
  145. '          number greater than BIG_FACT or less
  146. '          than zero is passed in (which results
  147. '          in 0.0).
  148. '************************************************
  149. '
  150. FUNCTION Factorial# (I%)
  151.  
  152. IF I = 0 THEN
  153.     F# = 1
  154. ELSE
  155.     IF (I > 0) AND (I <= BigFact) THEN
  156.         F# = 1
  157.         FOR k = 1 TO I
  158.             F# = F# * k
  159.         NEXT
  160.     ELSE
  161.         F# = 0
  162.     END IF
  163. END IF
  164.  
  165. Factorial# = F#
  166.  
  167. END FUNCTION
  168.  
  169. FUNCTION TDiff! (TA!, TB!)
  170.     TT! = TB! - TA!
  171.     IF TT! < 0 THEN TT! = TT! + SecDay&
  172.     TDiff = TT!
  173. END FUNCTION
  174.